# coding: utf8
from . import admin_blueprint
from flask import render_template, redirect, url_for, flash, session, request
from .forms import *
from flaskmovie.models import Admin
from functools import wraps


def check_login(f):
    @wraps(f)
    def wraps_func(*args, **kw):
        if session.get('admin') is None:
            return redirect(url_for('admin.login', next=request.url))
        return f(*args, **kw)

    return wraps_func


@admin_blueprint.route('/')
@admin_blueprint.route('/index/')
@check_login
def index():
    return render_template('admin/index.html')


@admin_blueprint.route('/login/', methods=['GET', 'POST'])
def login():
    form = LoginForm()
    if form.validate_on_submit():
        data = form.data
        admin = Admin.query.filter_by(name=data['account']).first()
        if not admin.check_pwd(data['pwd']):
            flash('密码错误!')
            return redirect(url_for('admin.login'))
        session['admin'] = data['account']
        return redirect(request.args.get('next') or url_for('admin.index'))
    return render_template('admin/login.html', form=form)


@admin_blueprint.route('/logout')
@check_login
def logout():
    session.pop('admin', None)
    return redirect(url_for('admin.login'))


@admin_blueprint.route('/pwd')
@check_login
def pwd():
    return render_template('admin/pwd.html')


@admin_blueprint.route('/tag_add', methods=['GET', 'POST'])
@check_login
def tag_add():
    form = TagForm()
    if form.validate_on_submit():
        data = form.data
        if form.check_exists_name(data.get('name')):
            flash('标签名称已经存在!', 'danger')
            return redirect(url_for('admin.tag_add'))
        tag = Tag(name=data['name'])
        db.session.add(tag)
        db.session.commit()
        flash('标签添加成功', 'success')
        return redirect(url_for('admin.tag_add'))
    return render_template('admin/tag/add.html', form=form)


@admin_blueprint.route('/tag_list/', defaults={'page': 1})
@admin_blueprint.route('/tag_list/<int:page>/')
@check_login
def tag_list(page):
    page_data = Tag.query.order_by(
        Tag.create_time.asc()
    ).paginate(page=page, per_page=1)

    return render_template('admin/tag/list.html', page_data=page_data)


@admin_blueprint.route('/movie_add')
@check_login
def movie_add():
    return render_template('admin/movie/add.html')


@admin_blueprint.route('/movie_list')
@check_login
def movie_list():
    return render_template('admin/movie/list.html')


@admin_blueprint.route('/preview_add')
@check_login
def preview_add():
    return render_template('admin/preview/add.html')


@admin_blueprint.route('/preview_list')
@check_login
def preview_list():
    return render_template('admin/preview/list.html')


@admin_blueprint.route('/user_list')
@check_login
def user_list():
    return render_template('admin/user/list.html')


@admin_blueprint.route('/user_view')
@check_login
def user_view():
    return render_template('admin/user/view.html')


@admin_blueprint.route('/comment_list')
@check_login
def comment_list():
    return render_template('admin/comment/list.html')


@admin_blueprint.route('/moviecol_list')
@check_login
def moviecol_list():
    return render_template('admin/moviecol/list.html')


@admin_blueprint.route('/oplog_list')
@check_login
def oplog_list():
    return render_template('admin/log/oplog_list.html')


@admin_blueprint.route('/adminloginlog_list')
def adminloginlog_list():
    return render_template('admin/log/adminloginlog_list.html')


@admin_blueprint.route('/userloginlog_list')
def userloginlog_list():
    return render_template('admin/log/userloginlog_list.html')


@admin_blueprint.route('/auth_add')
def auth_add():
    return render_template('admin/auth/add.html')


@admin_blueprint.route('/auth_list')
def auth_list():
    return render_template('admin/auth/list.html')


@admin_blueprint.route('/role_add')
def role_add():
    return render_template('admin/role/add.html')


@admin_blueprint.route('/role_list')
def role_list():
    return render_template('admin/role/list.html')


@admin_blueprint.route('/admin_add')
def admin_add():
    return render_template('admin/admin/add.html')


@admin_blueprint.route('/admin_list')
def admin_list():
    return render_template('admin/admin/list.html')
